package com.huawei.M01.iterator.M24;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 1630. 等差子数组
 *
 * @author 111
 */
public class CheckArithmetic {
    public List<Boolean> checkArithmeticSubarrays(int[] nums, int[] l, int[] r) {
        int n = l.length;
        List<Boolean> res = new ArrayList<>(n);
        for (int i = 0; i < n; i++) {
            int length = r[i] - l[i] + 1;
            int[] tmp = new int[length];
            System.arraycopy(nums, l[i], tmp, 0, length);
            res.add(isArithmeticSubarrays(tmp));
        }
        return res;
    }

    private Boolean isArithmeticSubarrays(int[] tmp) {
        if (tmp.length <= 2) {
            return true;
        }
        Arrays.sort(tmp);
        int dex = tmp[1] - tmp[0];
        for (int i = 2; i < tmp.length; i++) {
            if (tmp[i] - tmp[i - 1] != dex) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(new CheckArithmetic().checkArithmeticSubarrays(new int[]{4, 6, 5, 9, 3, 7}, new int[]{0, 0, 2}, new int[]{2, 3, 5}));
    }
}
